<!DOCTYPE html>
<html prefix="og: http://ogp.me/ns# article: http://ogp.me/ns/article# " lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>swift-codeblocks-syntax-highlight | 绿萝间</title>
<link href="../assets/css/all-nocdn.css" rel="stylesheet" type="text/css">
<link href="../assets/css/ipython.min.css" rel="stylesheet" type="text/css">
<link href="../assets/css/nikola_ipython.css" rel="stylesheet" type="text/css">
<meta name="theme-color" content="#5670d4">
<meta name="generator" content="Nikola (getnikola.com)">
<link rel="alternate" type="application/rss+xml" title="RSS" href="../rss.xml">
<link rel="canonical" href="https://muxuezi.github.io/posts/swift-codeblocks-syntax-highlight.html">
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"] ],
        displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
        processEscapes: true
    },
    displayAlign: 'center', // Change this to 'center' to center equations.
    "HTML-CSS": {
        styles: {'.MathJax_Display': {"margin": 0}}
    }
});
</script><!--[if lt IE 9]><script src="../assets/js/html5.js"></script><![endif]--><meta name="author" content="Tao Junjie">
<link rel="prev" href="china-trillion-dollars-wasted-investment.html" title="China-trillion-dollars-wasted-investment" type="text/html">
<link rel="next" href="swift-chs.html" title="Swift CHS" type="text/html">
<meta property="og:site_name" content="绿萝间">
<meta property="og:title" content="swift-codeblocks-syntax-highlight">
<meta property="og:url" content="https://muxuezi.github.io/posts/swift-codeblocks-syntax-highlight.html">
<meta property="og:description" content="Everthing is Ok now, thanks Samy Pessé.
Lastest version at The Swift Programming Language 中文版.
由于The Swift Programming Language 中文版不能swift语言高亮.
Enjoy：

Swift Codeblocks Syntax Highlight on Coding.

Sw">
<meta property="og:type" content="article">
<meta property="article:published_time" content="2015-06-24T13:44:10+08:00">
<meta property="article:tag" content="ipython">
<meta property="article:tag" content="Swift">
</head>
<body>
<a href="#content" class="sr-only sr-only-focusable">Skip to main content</a>

<!-- Menubar -->

<nav class="navbar navbar-inverse navbar-static-top"><div class="container">
<!-- This keeps the margins nice -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-navbar" aria-controls="bs-navbar" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="https://muxuezi.github.io/">

                <span id="blog-title">绿萝间</span>
            </a>
        </div>
<!-- /.navbar-header -->
        <div class="collapse navbar-collapse" id="bs-navbar" aria-expanded="false">
            <ul class="nav navbar-nav">
<li>
<a href="../archive.html">Archive</a>
                </li>
<li>
<a href="../categories/">Tags</a>
                </li>
<li>
<a href="../rss.xml">RSS feed</a>

                
            </li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
    <a href="swift-codeblocks-syntax-highlight.ipynb" id="sourcelink">Source</a>
    </li>

                
            </ul>
</div>
<!-- /.navbar-collapse -->
    </div>
<!-- /.container -->
</nav><!-- End of Menubar --><div class="container" id="content" role="main">
    <div class="body-content">
        <!--Body content-->
        <div class="row">
            
            
<article class="post-text h-entry hentry postpage" itemscope="itemscope" itemtype="http://schema.org/Article"><header><h1 class="p-name entry-title" itemprop="headline name"><a href="#" class="u-url">swift-codeblocks-syntax-highlight</a></h1>

        <div class="metadata">
            <p class="byline author vcard"><span class="byline-name fn">
                    Tao Junjie
            </span></p>
            <p class="dateline"><a href="#" rel="bookmark"><time class="published dt-published" datetime="2015-06-24T13:44:10+08:00" itemprop="datePublished" title="2015-06-24 13:44">2015-06-24 13:44</time></a></p>
            
        <p class="sourceline"><a href="swift-codeblocks-syntax-highlight.ipynb" id="sourcelink">Source</a></p>

        </div>
        

    </header><div class="e-content entry-content" itemprop="articleBody text">
    <div tabindex="-1" id="notebook" class="border-box-sizing">
    <div class="container" id="notebook-container">

<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Everthing is Ok now, thanks <a href="http://www.samypesse.fr/">Samy Pessé</a>.</p>
<p>Lastest version at <a href="http://numbbbbb.gitbooks.io/-the-swift-programming-language-/">The Swift Programming Language 中文版</a>.</p>
<p><del>由于<a href="http://numbbbbb.gitbooks.io/-the-swift-programming-language-/">The Swift Programming Language 中文版</a>不能swift语言高亮.</del></p>
<p>Enjoy：</p>
<ul>
<li>
<p><a href="http://swift.coding.io/">Swift Codeblocks Syntax Highlight</a> on <a href="https://coding.net/u/tj2/p/swift/git">Coding</a>.</p>
</li>
<li>
<p><a href="http://muxuezi.gitbooks.io/swift-codeblocks/">Swift Codeblocks Syntax Highlight</a> on <a href="https://github.com/muxuezi/swift-codeblocks/">Gitbhub</a>.</p>
</li>
</ul>
<!-- TEASER_END--><p><del>There's a swfit syntax bug highlight on<a href="https://www.gitbook.io" title="Gitbook">Gitbook</a>now, even though <a href="http://www.samypesse.fr/">Samy Pessé</a> had upgraded the highlight version of <a href="https://github.com/GitbookIO/gitbook">gitbook/package.json</a> to <a href="https://highlightjs.org/">8.1.0</a>, which support swfit syntax.</del></p>
<ul>
<li>
<a href="https://coding.net/" title="Coding">Coding</a>和<a href="https://www.gitbook.io" title="Gitbook">Gitbook</a>都通过<a href="https://highlightjs.org/usage/" title="highlightjs">highlightjs</a>支持swift语法高亮;</li>
<li>
<a href="https://coding.net/u/tj2/p/swift/git">Coding</a>和<a href="https://github.com/muxuezi/swift-codeblocks/">Github</a>可以在代码管理页面直接语法高亮;</li>
<li>
<a href="https://www.gitbook.io" title="Gitbook">Gitbook</a>还不行，如<a href="https://www.gitbook.io/book/muxuezi/swift-codeblocks">首页</a>不支持swift语法高亮。</li>
</ul>
<p>如果不论PDF、MOBI、EPUB格式输出，则
$$Coding \approx Github + Gitbook$$</p>
<p>一般html解决办法见<a href="https://highlightjs.org/usage/" title="highlightjs">highlightjs</a>，需要xcode.css、highlight.pack.js，并在head增加:</p>
<div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">link</span> <span class="na">rel</span><span class="o">=</span><span class="s">"stylesheet"</span> <span class="na">href</span><span class="o">=</span><span class="s">"../assets/css/xcode.css"</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">"../assets/js/highlight.pack.js"</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">script</span><span class="p">&gt;</span><span class="nx">hljs</span><span class="p">.</span><span class="nx">initHighlightingOnLoad</span><span class="p">();&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
</pre></div>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><link rel="stylesheet" href="../assets/css/xcode.css"></p>
<script src="../assets/js/highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script><p>Test:</p>
<div class="highlight"><pre><span></span><span class="kd">extension</span> <span class="nc">OCBool</span><span class="p">:</span> <span class="nb">Equatable</span><span class="p">{</span>
<span class="p">}</span>

<span class="c1">//支持等值判断运算符</span>
<span class="kd">func</span> <span class="p">==(</span> <span class="kr">left</span><span class="p">:</span> <span class="n">OCBool</span><span class="p">,</span> <span class="kr">right</span><span class="p">:</span> <span class="n">OCBool</span> <span class="p">)-&gt;</span><span class="nb">Bool</span><span class="p">{</span>
    <span class="k">switch</span> <span class="p">(</span><span class="kr">left</span><span class="p">,</span> <span class="kr">right</span><span class="p">){</span>
    <span class="k">case</span> <span class="p">(.</span><span class="n">ocTrue</span><span class="p">,</span> <span class="p">.</span><span class="n">ocTrue</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">true</span>
    <span class="k">default</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">false</span>
    <span class="p">}</span>
<span class="p">}</span>
<span class="c1">//支持位与运算符</span>
<span class="kd">func</span> <span class="o">&amp;</span><span class="p">(</span> <span class="kr">left</span><span class="p">:</span><span class="n">OCBool</span><span class="p">,</span> <span class="kr">right</span><span class="p">:</span> <span class="n">OCBool</span><span class="p">)-&gt;</span><span class="n">OCBool</span><span class="p">{</span>

    <span class="k">if</span> <span class="kr">left</span><span class="p">{</span>
        <span class="k">return</span> <span class="kr">right</span>
    <span class="p">}</span>
    <span class="k">else</span><span class="p">{</span>
        <span class="k">return</span> <span class="kc">false</span>
    <span class="p">}</span>
<span class="p">}</span>
<span class="c1">//支持位或运算符</span>
<span class="kd">func</span> <span class="o">|</span><span class="p">(</span> <span class="kr">left</span><span class="p">:</span><span class="n">OCBool</span><span class="p">,</span> <span class="kr">right</span><span class="p">:</span> <span class="n">OCBool</span><span class="p">)-&gt;</span><span class="n">OCBool</span><span class="p">{</span>

    <span class="k">if</span> <span class="kr">left</span><span class="p">{</span>
        <span class="k">return</span> <span class="kc">true</span>
    <span class="p">}</span>
    <span class="k">else</span><span class="p">{</span>
        <span class="k">return</span> <span class="kr">right</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">//支持位异或运算符</span>
<span class="kd">func</span> <span class="o">^</span><span class="p">(</span> <span class="kr">left</span><span class="p">:</span><span class="n">OCBool</span><span class="p">,</span> <span class="kr">right</span><span class="p">:</span> <span class="n">OCBool</span><span class="p">)-&gt;</span><span class="n">OCBool</span><span class="p">{</span>
    <span class="k">return</span> <span class="n">OCBool</span><span class="p">(</span> <span class="kr">left</span> <span class="o">!=</span> <span class="kr">right</span> <span class="p">)</span>
<span class="p">}</span>
<span class="c1">//支持求反运算符</span>
<span class="p">@</span><span class="kr">prefix</span> <span class="kd">func</span> <span class="o">!</span><span class="p">(</span> <span class="n">a</span><span class="p">:</span><span class="n">OCBool</span> <span class="p">)-&gt;</span> <span class="n">OCBool</span><span class="p">{</span>
    <span class="k">return</span> <span class="n">a</span> <span class="o">^</span> <span class="kc">true</span>
<span class="p">}</span>
<span class="c1">//支持组合求与运算符</span>
<span class="kd">func</span> <span class="o">&amp;=</span> <span class="p">(</span><span class="kr">inout</span> <span class="kr">left</span><span class="p">:</span><span class="n">OCBool</span><span class="p">,</span> <span class="kr">right</span><span class="p">:</span><span class="n">OCBool</span> <span class="p">){</span>
    <span class="kr">left</span> <span class="p">=</span> <span class="kr">left</span> <span class="o">&amp;</span> <span class="kr">right</span>
<span class="p">}</span>


<span class="kd">var</span> <span class="nv">isHasMoney</span><span class="p">:</span><span class="n">OCBool</span> <span class="p">=</span> <span class="kc">true</span>
<span class="kd">var</span> <span class="nv">isHasWife</span><span class="p">:</span><span class="n">OCBool</span> <span class="p">=</span> <span class="kc">true</span>
<span class="kd">var</span> <span class="nv">isHasHealty</span><span class="p">:</span><span class="n">OCBool</span> <span class="p">=</span> <span class="kc">true</span>
<span class="kd">var</span> <span class="nv">isHasLover</span><span class="p">:</span><span class="n">OCBool</span> <span class="p">=</span> <span class="kc">true</span>

<span class="n">isHasMoney</span> <span class="o">!=</span> <span class="n">isHasHealty</span>
<span class="n">isHasHealty</span> <span class="p">==</span> <span class="n">isHasMoney</span>
<span class="n">isHasWife</span> <span class="o">^</span> <span class="n">isHasLover</span>
<span class="n">isHasWife</span> <span class="p">=</span> <span class="o">!</span><span class="n">isHasLover</span>

<span class="k">if</span> <span class="p">(</span><span class="n">isHasMoney</span> <span class="o">|</span> <span class="n">isHasHealty</span><span class="p">)</span> <span class="o">&amp;</span> <span class="n">isHasHealty</span><span class="p">{</span>
    <span class="bp">println</span><span class="p">(</span> <span class="s">"坐看云起时"</span><span class="p">)</span>
<span class="p">}</span><span class="k">else</span>
<span class="p">{</span>
    <span class="bp">println</span><span class="p">(</span><span class="s">"千金散尽还复来"</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
<p><a href="https://www.gitbook.io/book/muxuezi/swift-codeblocks/activity"><img src="https://www.gitbook.io/button/status/book/muxuezi/swift-codeblocks" alt="Build Status"></a></p>

</div>
</div>
</div>
    </div>
  </div>

    </div>
    <aside class="postpromonav"><nav><ul itemprop="keywords" class="tags">
<li><a class="tag p-category" href="../categories/ipython.html" rel="tag">ipython</a></li>
            <li><a class="tag p-category" href="../categories/swift.html" rel="tag">Swift</a></li>
        </ul>
<ul class="pager hidden-print">
<li class="previous">
                <a href="china-trillion-dollars-wasted-investment.html" rel="prev" title="China-trillion-dollars-wasted-investment">Previous post</a>
            </li>
            <li class="next">
                <a href="swift-chs.html" rel="next" title="Swift CHS">Next post</a>
            </li>
        </ul></nav></aside><script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"> </script><script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"] ],
        displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
        processEscapes: true
    },
    displayAlign: 'center', // Change this to 'center' to center equations.
    "HTML-CSS": {
        styles: {'.MathJax_Display': {"margin": 0}}
    }
});
</script></article>
</div>
        <!--End of body content-->

        <footer id="footer">
            Contents © 2017         <a href="mailto:muxuezi@gmail.com">Tao Junjie</a> - Powered by         <a href="https://getnikola.com" rel="nofollow">Nikola</a>         
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0">
<img alt="Creative Commons License BY-NC-SA" style="border-width:0; margin-bottom:12px;" src="http://i.creativecommons.org/l/by-nc-sa/4.0/80x15.png"></a>
            
        </footer>
</div>
</div>


            <script src="../assets/js/all-nocdn.js"></script><script>$('a.image-reference:not(.islink) img:not(.islink)').parent().colorbox({rel:"gal",maxWidth:"100%",maxHeight:"100%",scalePhotos:true});</script><!-- fancy dates --><script>
    moment.locale("en");
    fancydates(0, "YYYY-MM-DD HH:mm");
    </script><!-- end fancy dates --><script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-51330059-1', 'auto');
  ga('send', 'pageview');

</script>
</body>
</html>
